

// 将起点附近的点按距离顺序排列
function getNearPoint() {
	nPoints = [];
	var pArray = [];
	for(var i = 0; i < signalPoints.length; i++) {
		var distance = Math.sqrt(Math.pow(signalPoints[i][0] - sPoint.x, 2) + Math.pow(signalPoints[i][1] - sPoint.y, 2));
		distance = distance.toFixed(2)*1;
		var nPoint = {
			x: signalPoints[i][0],
			y: signalPoints[i][1],
			d: distance
		};
		pArray.push(nPoint);
	}
	pArray.sort(function(a, b) {
		return a.d - b.d;
	});
	for(let i = 0; i < 4; i++) {
		nPoints.push(pArray[i]);
	}
	printNearPoint(nPoints);
}

// 调用三点定位定位起点
function getSPoint() {
	var calPoint = ThreePointOrientation(nPoints[0], nPoints[1], nPoints[2]);
	if(calPoint.length == 1) {
		updateSpan('#TPO', "三点定位到的起点坐标为:(" + calPoint[0].x + "," + calPoint[0].y + ")");
	} else if(calPoint.length == 2) {
		var content = '';
		for(let i = 0; i < calPoint.length; i++) {
			content += "(" + calPoint[i].x + "," + calPoint[i].y + ")";
		}
		updateSpan('#TPO', "三点定位到的起点坐标有两个分别为:" + content);
	}
}

// 三点定位
function ThreePointOrientation(A, B, C) {
	var a = [A, B, C];
	var delta = -1;
	var intersects = [];
	intersect(A, B);
	checkPoint(C);
	return intersects;

	// 计算两个圆的交点
	function intersect(AP, BP) {
		var a, b, c;
		var x_1, x_2, y_1, y_2;
		var delta = -1;
		var x1 = AP.x,
			y1 = AP.y,
			r1 = AP.d;
		var x2 = BP.x,
			y2 = BP.y,
			r2 = BP.d;
		if(y1 != y2) {
			var A = (x1 * x1 - x2 * x2 + y1 * y1 - y2 * y2 + r2 * r2 - r1 * r1) / (2 * (y1 - y2));
			var B = (x1 - x2) / (y1 - y2);
			a = 1 + B * B;
			b = -2 * (x1 + (A - y1) * B);
			c = x1 * x1 + (A - y1) * (A - y1) - r1 * r1;
			delta = b * b - 4 * a * c;
			if(delta > 0) {

				x_1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
				x_2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
				y_1 = A - B * x_1;
				y_2 = A - B * x_2;
			} else if(delta == 0) {
				x_1 = x_2 = -b / (2 * a);
				y_1 = y_2 = A - B * x_1;
			} else {
				console.log("两个圆不相交");
				return null;
			}
		} else if(x1 != x2) {
			//当y1=y2时，x的两个解相等
			x_1 = x_2 = (x1 * x1 - x2 * x2 + r2 * r2 - r1 * r1) / (2 * (x1 - x2));

			a = 1;
			b = -2 * y1;
			c = y1 * y1 - r1 * r1 + (x_1 - x1) * (x_1 - x1);

			delta = b * b - 4 * a * c;

			if(delta > 0) {
				y_1 = (-b + Math.sqrt(b * b - 4 * a * c)) / (2 * a);
				y_2 = (-b - Math.sqrt(b * b - 4 * a * c)) / (2 * a);
			} else if(delta == 0) {
				y_1 = y_2 = -b / (2 * a);
			} else {
				console.log("两个圆不相交");
				return null;
			}
		} else {
			console.log("无解");
			return null;
		}
		var intersect1 = Point.createNew(x_1.toFixed(2)*1, y_1.toFixed(2)*1);
		var intersect2 = Point.createNew(x_2.toFixed(2)*1, y_2.toFixed(2)*1);
		intersects.push(intersect1);
		if(delta != 0) {
			intersects.push(intersect2);
		}
	}

	// 将两个圆的交点代入第三个圆计算三个圆的交点
	function checkPoint(a) {
		var temp = [];
		for(let i = 0; i < intersects.length; i++) {
			var d1 = Math.sqrt(Math.pow(intersects[i].x - a.x, 2) + Math.pow(intersects[i].y - a.y, 2));
			d1 = d1.toFixed(2)*1;
			if(d1 == a.d) {
				temp.push(intersects[i]);
			}
		}
		intersects = temp.concat();
	}
}